#!/usr/bin/env python

import sys
import pandas as pd
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

tsv_path = sys.argv[1]

df = pd.read_csv(tsv_path, parse_dates=True)
df['timestamp'] = pd.to_datetime(df['timestamp'])
df = df.set_index('timestamp')

plt.figure(figsize=(60, 30))

groups = df.groupby('label')
dfs = {}
for key, group in groups:
  dfs[key] = group['price']

plt.subplot(2, 1, 1)
plt.title('PnL')
rolling_krw = dfs['KRW'].rolling(5).median()
rolling_usdt = dfs['USDT'].rolling(5).median()
rolling_krw = (rolling_krw - rolling_krw.iloc[5]).iloc[5:]
rolling_usdt = (rolling_usdt - rolling_usdt.iloc[5]).iloc[5:]

plt.plot(rolling_usdt + rolling_krw / 1064., marker=None, label='PnL')

plt.subplot(2, 1, 2)

plt.title('Trade')
plt.plot(dfs['GMEAN_PRICE'], marker=None, label='PnL')
plt.plot(dfs['EMA_PRICE'], marker=None, label='PnL')
plt.plot(dfs['BUY'], marker='x', ls='None', label='PnL')
plt.plot(dfs['SELL'], marker='x', ls='None', label='PnL')

plt.savefig('output.png')
